package com.bootdo.common.utils;

import com.bootdo.common.config.ApplicationContextRegister;
import com.bootdo.system.dao.UserDao;
import com.bootdo.system.dao.UserRoleDao;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import com.bootdo.system.domain.UserDO;

import java.util.List;

public class ShiroUtils {

    public static Subject getSubjct() {
        return SecurityUtils.getSubject();
    }
    public static UserDO getUser() {
        Long userId = (Long) getSubjct().getPrincipal();
        UserDao userMapper = ApplicationContextRegister.getBean(UserDao.class);
        UserDO user = userMapper.get(userId);
        //获取权限
        if (user != null) {
            UserRoleDao userRoleMapper = ApplicationContextRegister.getBean(UserRoleDao.class);
            List<Long> rolesIds = userRoleMapper.listRoleId(user.getUserId());
            user.setRoleIds(rolesIds);
        }
        return user;
    }
    public static Long getUserId() {
        return getUser().getUserId();
    }
    public static void logout() {
        getSubjct().logout();
    }

}
